using System;

namespace Randomize.Core.Generators
{
    public class DateTimeGenerator : RangeGenerator<DateTime>, IDateTimeGenerator
    {
        public bool IgnoreTime { get; set; }

        public DateTimeGenerator(Random random) : base(random)
        {
        }

        protected override DateTime GenerateRandomValue()
        {
            var from = this.Min ?? DateTime.MinValue;
            var to = this.Max ?? DateTime.MaxValue;

            var generated = from + new TimeSpan((long) (this.RandomInstance.NextDouble()*(to.Ticks - from.Ticks)));

            if (this.IgnoreTime)
            {
                DateTime today = new DateTime(generated.Year, generated.Month, generated.Day);
                TimeSpan fromToday = generated - today;
                DateTime tomorrow = today.AddDays(1);
                TimeSpan fromTomorrow = tomorrow - generated;

                generated = fromTomorrow < fromToday ? tomorrow : today;
            }

            return generated;
        }
    }
}